/*
 * 
 * v1.2 by tonydon
 */
package cn.txdnet.txcms.admin.action;

import cn.txdnet.txcms.domain.Infobase;
import cn.txdnet.txcms.domain.Manager;
import cn.txdnet.txcms.query.InfobaseQuery;
import cn.txdnet.txcms.service.InfobaseService;
import cn.txdnet.txcms.service.InfotagsService;
import cn.txdnet.txweb.annotation.methods.PostMethod;
import cn.txdnet.txweb.model.ModelUtil;
import cn.txdnet.txweb.model.ModelView;
import cn.txdnet.txweb.utils.BeanUtil;
import cn.txdnet.txweb.utils.Message;
import cn.txdnet.txweb.utils.SessionUtil;
import cn.txdnet.txweb.utils.WebUtil;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author txdnet
 */
public class InfobaseAction {

    /**
     * 保存新信息
     *
     * @param req
     * @return
     * @throws Exception
     */
    @PostMethod
    public ModelView save(HttpServletRequest req) throws Exception {
        Infobase info = BeanUtil.parseRequestToBean(Infobase.class, req);
        String tagids = WebUtil.getArrStr(req, "tagids", null);
        if (!info.validate()) {
            return ModelUtil.getTextModel(Message.MSG_FIELD_ERR);
        }
        Manager mgr = SessionUtil.getManager(req);
        info.setPid(mgr.getMgrid().longValue());
        if (InfobaseService.make(info)) {
            // 更新infotags
            InfotagsService.insert(info.getInfoid(), tagids);
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量删除信息
     *
     * @param req
     * @return
     */
    public ModelView delete_batch(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        if (ids == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        Manager mgr = SessionUtil.getManager(req);
        int del_num = InfobaseService.deleteBatch(ids, mgr.getMgrid().longValue());
        if (del_num > 0) {
            // 更新infotags
            InfotagsService.delete(ids);
            return ModelUtil.getTextModel(del_num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新发布状态
     *
     * @param req
     * @return
     */
    public ModelView update_pubstate(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        byte pubstate = WebUtil.getByte(req, "pubstate", -1);
        if (ids == null || pubstate < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = InfobaseService.updatePubstateBatch(ids, pubstate);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新评论状态
     *
     * @param req
     * @return
     */
    public ModelView update_cmtstate(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        byte iscmt = WebUtil.getByte(req, "iscmt", -1);
        if (ids == null || iscmt < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = InfobaseService.updateCmtstateBatch(ids, iscmt);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新标识状态
     *
     * @param req
     * @return
     */
    public ModelView update_tflag(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        byte tflag = WebUtil.getByte(req, "tflag", -1);
        if (ids == null || tflag < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = InfobaseService.updateTFlagBatch(ids, tflag);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新标题颜色
     *
     * @param req
     * @return
     */
    public ModelView update_tcolor(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        String tcolor = WebUtil.getNoNull(req, "tcolor", "-");
        if (ids == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = InfobaseService.updateTColorBatch(ids, tcolor);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新发布时间
     *
     * @param req
     * @return
     */
    public ModelView update_pubtime(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        long pubtime = WebUtil.getLong(req, "pubtime", -1);
        if (ids == null || pubtime < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = InfobaseService.updatePubTimeBatch(ids, pubtime);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新结束时间
     *
     * @param req
     * @return
     */
    public ModelView update_endtime(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        long endtime = WebUtil.getLong(req, "endtime", 0);
        if (ids == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = InfobaseService.updateEndTimeBatch(ids, endtime);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更新信息权重
     *
     * @param req
     * @return
     */
    public ModelView update_weight(HttpServletRequest req) {
        long infoid = WebUtil.getLong(req, "infoid", -1);
        int weight = WebUtil.getInt(req, "weight", 0);
        if (infoid < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = InfobaseService.updateWeight(infoid, weight);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新信息频道和类目信息
     *
     * @param req
     * @return
     */
    public ModelView update_channel_catelog(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        int cid = WebUtil.getInt(req, "cid", -1);
        String ctp = WebUtil.getNoNull(req, "ctp", null);
        byte channeltype = WebUtil.getByte(req, "channeltype", -1);
        if (ids == null || cid < 0 || ctp == null || channeltype < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = InfobaseService.updateChannelAndCatalogBatch(ids, channeltype, cid, ctp);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 查询信息
     *
     * @param req
     * @return
     */
    public ModelView list_factor(HttpServletRequest req) throws Exception {
        InfobaseQuery query = BeanUtil.parseRequestToBean(InfobaseQuery.class, req);
        Manager mgr = SessionUtil.getManager(req);
        query.setPid(mgr.getMgrid().longValue());
        return ModelUtil.getJsonModel(InfobaseService.queryByFactor(query));
    }

    /**
     * 得到信息权重最大值
     *
     * @return
     */
    public ModelView query_max_weight() {
        return ModelUtil.getTextModel(InfobaseService.queryMaxWeight());
    }

    /**
     * 初始化信息编辑表单界面
     *
     * @param req
     * @return
     */
    public ModelView query_infobase_edit(HttpServletRequest req) {
        long infoid = WebUtil.getLong(req, "infoid", -1);
        if (infoid < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        Infobase infobase = InfobaseService.queryInfobaseForEdit(infoid);
        if (infobase == null) {
            return ModelUtil.getTextModel(Message.MSG_VALUE_NULL);
        }
        return ModelUtil.getJsonModel(infobase);
    }

    /**
     * 更新信息数据
     *
     * @param req
     * @return
     * @throws Exception
     */
    @PostMethod
    public ModelView update(HttpServletRequest req) throws Exception {
        Infobase info = BeanUtil.parseRequestToBean(Infobase.class, req);
        String tagids = WebUtil.getArrStr(req, "tagids", null);
        if (!info.validate()) {
            return ModelUtil.getTextModel(Message.MSG_FIELD_ERR);
        }
        if (InfobaseService.update(info)) {
            // 更新infotags
            InfotagsService.update(info.getInfoid(), tagids);
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }
}
